// pages/centerOrder/centerOrder.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userOrder:[],
    orderid:'',
    delorderid:'',
    delorderstate:'',
    receivingid:'',
    receivingstate:'',
    receivingoverid:'',
    refundid:'',
    refundstate:'',
    evaluatename:'',
    paymentname:'',
    evaluateimg:'',
    paymentimg:'',
    orderConn:'',
    orderuserid:'',
    orderiddd:'',
    plcon:'',
    evaluatearr:'',
    paymentarr:'',
    value:'',
    paypwdnum:'',
    evaluatebox: false,
    evaluateboxx: false,
    upMsg:'',
    downMsg:'',


    show:false,//控制下拉列表的显示隐藏，false隐藏、true显示
    selectData:['1','2','3','4','5','6'],//下拉列表的数据
    index:0//选择的下拉列表下标
  },
  onLoad:function(){
    //获取用户id发送请求
    var userid = wx.getStorageSync("OoOoUserid");
    var data = {
      userid:userid,
    }
    var that = this;
    //获取订单
    wx.request({
      url:'http://47.104.142.216/TP/public/index.php/index/Usercenter/selectOrder',
      dataType:'json',
      method:'POST',
      data:data,
      header:{
        'content-type':'application/x-www-form-urlencoded'
      },
      success:function(res){
          that.setData({
            //第一个data为固定用法
            userOrder: res.data
          })
        },
        fail:function(err){
        console.log(err)
        }
    });
  },
  //升序
  up:function(e){
    var that = this;
    var upMsg = e.target.dataset;
    var newupMsg = upMsg.upp;
     //获取用户id发送请求
     var userid = wx.getStorageSync("OoOoUserid");
     var data = {
       userid:userid,
       up:newupMsg,
     }
     var that = this;
     //获取订单
     wx.request({
       url:'http://47.104.142.216/TP/public/index.php/index/Usercenter/selectOrder',
       dataType:'json',
       method:'POST',
       data:data,
       header:{
         'content-type':'application/x-www-form-urlencoded'
       },
       success:function(res){
           that.setData({
             //第一个data为固定用法
             userOrder: res.data
           })
          
         },
         fail:function(err){
         console.log(err)
         }
     });
  },
  //降序
  down:function(e){
    var downMsg = e.target.dataset;
    var newdownMsg = downMsg.downn;
    //获取用户id发送请求
    var userid = wx.getStorageSync("OoOoUserid");
    var data = {
      userid:userid,
      down:newdownMsg,
    }
    var that = this;
    //获取订单
    wx.request({
      url:'http://47.104.142.216/TP/public/index.php/index/Usercenter/selectOrder',
      dataType:'json',
      method:'POST',
      data:data,
      header:{
        'content-type':'application/x-www-form-urlencoded'
      },
      success:function(res){
          that.setData({
            //第一个data为固定用法
            userOrder: res.data
          })
        },
        fail:function(err){
        console.log(err)
        }
    });
  },
  //催发货
  expeditegoods:function(){
    wx.showToast({
      title: '提醒成功！', // 标题
      icon: 'success',  // 图标类型，默认success
      duration: 1500  // 提示窗停留时间，默认1500ms
    })
    return;
  },
  //退款
  refund:function(e){
    var that = this;
    this.setData({
      refundid : e.currentTarget.dataset.refundid,
      refundstate : e.currentTarget.dataset.refundstate,
    })
    var refundId = this.data.refundid;
    var refundState = this.data.refundstate;
    var newrefundState = parseInt(refundState) +1;
    wx.showModal({
      title: '提示',
      content: '确定要退款吗？',
      success (res) {
        if (res.confirm) {
          var refundID = refundId;
          var newrefundSTate = newrefundState;
          var data = {
            tkid : refundID,
            newtkstate : newrefundSTate,
          }
          //退款
          wx.request({
            url:'http://47.104.142.216/TP/public/index.php/index/Usercenter/refundOK',
            dataType:'json',
            method:'POST',
            data:data,
            header:{
              'content-type':'application/x-www-form-urlencoded'
            },
            success:function(res){
              if(res.data == 10000){
                wx.showToast({
                  title: '已申请退款！',
                  icon: 'success',
                  duration: 2000
                })
                that.onLoad()
                return;
            }
              },
              fail:function(err){
              console.log(err)
              }
          });
        } else if (res.cancel) {

        }
      }
    })
  },
  //删除订单
  Deleteorder:function(e){
    var that = this;
    this.setData({
      delorderid : e.currentTarget.dataset.delorderid,
      delorderstate : e.currentTarget.dataset.delorderstate,
    })
    var delorderid = this.data.delorderid;
    var delorderstate = this.data.delorderstate;
    wx.showModal({
      title: '提示',
      content: '确定要删除订单吗？',
      success (res) {
        if (res.confirm) {
          var orderID = delorderid;
          var delorderState = delorderstate;
          var data = {
            orderID : orderID,
            delstate : delorderState,
          }
          wx.request({
            url:'http://47.104.142.216/TP/public/index.php/index/Usercenter/delOrder',
            dataType:'json',
            method:'POST',
            data:data,
            header:{
              'content-type':'application/x-www-form-urlencoded'
            },
            success:function(res){
                if(res.data == 10000){
                  wx.showToast({
                    title: '订单已删除',
                    icon: 'success',
                    duration: 2000
                  })
                  that.onLoad()
                  return;
              }
            },
              fail:function(err){
              console.log(err)
              }
          });

        } else if (res.cancel) {

        }
      }
    })
  },
    //已退款删除订单
    Deleteorderrr:function(e){
      var that = this;
      this.setData({
        delorderid : e.currentTarget.dataset.delorderid,
        delorderstate : e.currentTarget.dataset.delorderstate,
      })
      var delorderid = this.data.delorderid;
      var delorderstate = this.data.delorderstate;
      var newdelorderstate = parseInt(delorderstate) -2;
      wx.showModal({
        title: '提示',
        content: '确定要删除订单吗？',
        success (res) {
          if (res.confirm) {
            var orderID = delorderid;
            var delorderState = newdelorderstate;
            var data = {
              orderID : orderID,
              delstate : delorderState,
            }
            wx.request({
              url:'http://47.104.142.216/TP/public/index.php/index/Usercenter/delOrder',
              dataType:'json',
              method:'POST',
              data:data,
              header:{
                'content-type':'application/x-www-form-urlencoded'
              },
              success:function(res){
                  if(res.data == 10000){
                    wx.showToast({
                      title: '订单已删除',
                      icon: 'success',
                      duration: 2000
                    })
                    that.onLoad()
                    return;
                }
              },
                fail:function(err){
                console.log(err)
                }
            });
  
          } else if (res.cancel) {
  
          }
        }
      })
    },
  //取消订单
  cancelorder:function(e){
    var that = this;
    this.setData({
      orderid : e.currentTarget.dataset.orderid,
      orderstate : e.currentTarget.dataset.orderstate,
   });
   var orderid =this.data.orderid;
   var orderstate =this.data.orderstate;
    wx.showModal({
      title: '提示',
      content: '确定要取消订单吗？',
      success (res) {
        if (res.confirm) {
          var orderId = orderid;
          var ordersTate = orderstate;
          var newordersTate = parseInt(ordersTate) + 1;
          //重新获取
          var data = {
            quxiaoOrderId : orderId,
            newquxiaoOrderState : newordersTate,
          }
          //取消订单
          wx.request({
            url:'http://47.104.142.216/TP/public/index.php/index/Usercenter/cancelaOrder',
            dataType:'json',
            method:'POST',
            data:data,
            header:{
              'content-type':'application/x-www-form-urlencoded'
            },
            success:function(res){
              if(res.data == 10000){
                wx.showToast({
                  title: '已取消',
                  icon: 'success',
                  duration: 2000
                })
                that.onLoad()
                return;
              }
            },
              fail:function(err){
              console.log(err)
              }
          });

        } else if (res.cancel) {

        }
      }
    })
  },
  //确认收货
  confirmreceipt:function(e){
    var that = this;
    this.setData({
      receivingid : e.currentTarget.dataset.receivingid,
      receivingstate : e.currentTarget.dataset.receivingstate,
      receivingoverid : e.currentTarget.dataset.receivingoverid,
    })
    var receivingId = this.data.receivingid;
    var receivingState = this.data.receivingstate;
    var receivingOverid = this.data.receivingoverid;
    var newreceivingState = parseInt(receivingState) +1;
    var newreceivingOverid = parseInt(receivingOverid) +1;
    wx.showModal({
      title: '提示',
      content: '确定要收货吗？',
      success (res) {
        if (res.confirm) {
          var receivingid = receivingId;
          var receivingstate = newreceivingState;
          var receivingoverid = newreceivingOverid;

          var data = {
            querenid : receivingid,
            newquerenstate : receivingstate,
            newquerenoverid : receivingoverid,
          }
          //确认收货
          wx.request({
            url:'http://47.104.142.216/TP/public/index.php/index/Usercenter/confirmReceiptt',
            dataType:'json',
            method:'POST',
            data:data,
            header:{
              'content-type':'application/x-www-form-urlencoded'
            },
            success:function(res){
              if(res.data == 10000){
                wx.showToast({
                  title: '收获成功',
                  icon: 'success',
                  duration: 2000
                })
                that.onLoad()
                return;
              }
            },
              fail:function(err){
              console.log(err)
              }
          });

        } else if (res.cancel) {

        }
      }
    })
  },
  //评分框
  onChangeShowState: function (e) {
    this.setData({
      evaluatebox: (!this.data.evaluatebox),
      evaluateimg : e.currentTarget.dataset.evaluateimg,
      evaluatename : e.currentTarget.dataset.evaluatename,
      evaluatearr : e.currentTarget.dataset.evaluatearr,
    })
  },
  //支付输入支付密码框
  onChangeShowpayment: function(e){
    this.setData({
      evaluateboxx: (!this.data.evaluateboxx),
      paymentimg : e.currentTarget.dataset.paymentimg,
      paymentname : e.currentTarget.dataset.paymentname,
      paymentarr : e.currentTarget.dataset.paymentarr,
    })
  },

   // 点击下拉显示框
   selectTap(){
    this.setData({
      show: !this.data.show
    });
  },
  // 点击下拉列表
  optionTap(e){
    let Index=e.currentTarget.dataset.index;//获取点击的下拉列表的下标
    this.setData({
      index:Index,
      show:!this.data.show
    });
  },
  //input数字类型正则
  handleInput(e) {
    let value = this.validateNumber(e.detail.value)
    this.setData({
      value
    })
  },
  //支付密码正则
  bindpaymentCon(e) {
    let paypwdnum = this.validateNumber(e.detail.value)
    this.setData({
      paypwdnum
    })
  },
  //获取评论内容
  bindorderCon(e){
    let orderConn = e.detail.value
    this.setData({
      orderConn
    })
  },
  validateNumber(val) {
    return val.replace(/\D/g, '')
  },
   //提交评分
   ordersubmit:function(e){
     var that = this;
     if(this.data.orderConn == ''){
        wx.showToast({
          title: '评论内容不能为空',
          icon: 'none',
          duration: 2000
        })
        return;
     }else if(this.data.value == ''){
        wx.showToast({
          title: '请为店铺评个分吧！',
          icon: 'none',
          duration: 2000
        })
        return;
     }else if(this.data.value > 10){
        wx.showToast({
          title: '店铺满分为10分哦！',
          icon: 'none',
          duration: 2000
        })
        return;
     }
    this.setData({
      plcon : e.currentTarget.dataset.plcon,
    })
    var orderfS = this.data.index; //旧商品分数
    var neworderfS = parseInt(orderfS) +1; //新商品分数
    var uesrarr = this.data.evaluatearr; //数据数组
    var uesrid = uesrarr.order_user_id;   //用户id
    var Orderid = uesrarr.order_id;  //订单id
    var dppf = this.data.value; //店铺评分
    var sppjnr = this.data.orderConn; //商品评价内容
    
    var data = {
      userid : uesrid,
      shopvalue : neworderfS,
      storevalue : dppf,
      textarea2 : sppjnr,
      orderid : Orderid,
    }
    //发送评价
    wx.request({
      url:'http://47.104.142.216/TP/public/index.php/index/Usercenter/addevaluate',
      dataType:'json',
      method:'POST',
      data:data,
      header:{
        'content-type':'application/x-www-form-urlencoded'
      },
      success:function(res){
        if(res.data == 10000){
          wx.showToast({
            title: '评论成功',
            icon: 'success',
            duration: 2000
          })
          that.onLoad()
          return;
        }
      },
        fail:function(err){
        console.log(err)
        }
    });
  },
  //支付提交
  paymentsubmit:function(e){
    var that = this;
    if(this.data.paypwdnum == ''){
      wx.showToast({
        title: '请输入支付密码',
        icon: 'none',
        duration: 2000
      })
      return;
    }else if(this.data.paypwdnum.length < 6){
      wx.showToast({
        title: '请输入6位数支付密码！',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    var uesrarr = this.data.paymentarr; //数据数组

    var paypwdnum = this.data.paypwdnum; //支付密码
    var userid = uesrarr.order_user_id; //用户id
    var shoppingmoney = uesrarr.commodity_price; //商品价格
    var shoppingstate = uesrarr.order_status; //商品订单状态
    var shoppingid = uesrarr.order_id; //商品订单id
    var data = {
      userid : userid,
      pospwd : paypwdnum,
      shoppingmoney : shoppingmoney,
      shoppingstate: shoppingstate,
      shoppingid : shoppingid,
    }
    //支付提交
    wx.request({
      url:'http://47.104.142.216/TP/public/index.php/index/Usercenter/Paymoney',
      dataType:'json',
      method:'POST',
      data:data,
      header:{
        'content-type':'application/x-www-form-urlencoded'
      },
      success:function(res){
        if(res.data == 10001){
          wx.showToast({
            title: '交易密码错误',
            icon: 'none',
            duration: 2000
          })
          return;
        }else if(res.data == 10002){
            wx.showToast({
              title: '余额不足，请充值！',
              icon: 'none',
              duration: 2000
            })
          return;
        }else if(res.data == 10000){
          wx.showToast({
            title: '支付成功！',
            icon: 'success',
            duration: 2000
          })
          that.onLoad()
          return;
        }
      },
        fail:function(err){
        console.log(err)
        }
    });
  },

  //订单商品跳转
  toGoods: function(e){
    let that = this;
    let collectid = e.currentTarget.dataset.name;
    wx.redirectTo({
      url: '/pages/goods/goods?cid='+collectid,
    })
  }
})